//
//  BBContainerInfoNodeView.m
//  xNose
//
//  Created by ben smith on 13/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBContainerInfoNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBImageButtonFactory.h"
#import "BBContainerInfoView.h"

@implementation BBContainerInfoNodeView


-(void)addInfoButton
{
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	NSRect contentRect = [backgroundRect bounds];
	float yOffset = NSMaxY(contentRect) - (buttonSize) - borderHeight;
	NSRect buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - (borderHeight * 2.0), yOffset, buttonSize, buttonSize);
	
	NSButton * button = [BBImageButtonFactory checkButtonInRect:buttonRect];
	[button setTarget:self];
	[button setAction:@selector(checkClick)];
	[button setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
	[self addSubview:button];
	self.infoButton = button;
}


-(void)checkClick
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBCloseInfoNotification" object:self];
}


-(void)drawNode
{
	[self setupBackgroundRect];
	[self.backgroundGradient drawInBezierPath:backgroundRect angle:90.0];		
	
	NSRect containRect = [backgroundRect bounds];
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];
	
	[[NSColor grayColor] set];
	[backgroundRect stroke];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(containRect), NSMaxY(containRect) - titleHeight) toPoint:NSMakePoint(NSMaxX(containRect), NSMaxY(containRect) - titleHeight)];
	
	
	//[[NSColor yellowColor] set];
	//[NSBezierPath strokeRect:[containerInfoView frame]];
	//[node.title drawInRect:[self textRect] withAttributes:textAttributes];
	
  //super duper sketchy
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	NSRect contentRect = [backgroundRect bounds];
	float yOffset = NSMaxY(contentRect) - (buttonSize) - borderHeight;
	NSRect buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - (borderHeight * 2.0), yOffset, buttonSize, buttonSize);
	
	[infoButton setFrame:buttonRect];	
	[titleField setFrame:[self textRect]];

	// this is double plus sketchtastic! so the not right way to do this, but it must work soon! or death!
	NSRect f = NSInsetRect([self bounds], 3, 10);//[self bounds];
	f.size.height = NSHeight(f) - borderHeight - titleHeight;
//	infoFrame.origin.y = NSMaxX([self bounds]) - borderHeight - titleHeight - 3 - NSHeight(infoFrame);	
//	[containerInfoView setFrame:f];
}


-(void)setNode:(BBNode*)aNode
{
	[aNode retain];
	[node release];
	node = aNode;
	if (node != nil)
		[titleField setStringValue:node.title];
}

-(void)setupInfoView
{
	NSRect f = NSInsetRect([self bounds], 3, 10);//[self bounds];
	
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];
	
	f.size.height = NSHeight(f) - borderHeight - titleHeight;
	
	containerInfoView = [[BBContainerInfoView alloc] initWithFrame:f];
	[containerInfoView setNode:self.node];
	[[self animator] addSubview:containerInfoView];	
}

-(void)resetsubFrames
{
	NSRect f = NSInsetRect([self bounds], 3, 10);//[self bounds];
	[containerInfoView removeFromSuperview];
	[containerInfoView release];
	
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];
	
	f.size.height = NSHeight(f) - borderHeight - titleHeight;
	containerInfoView = [[BBContainerInfoView alloc] initWithFrame:f];
	[containerInfoView setNode:self.node];
	[self addSubview:containerInfoView];	
}

- (void)setupTextAttributes
{
	titleField = [[NSTextField alloc] initWithFrame:[self textRect]];
	[titleField setAlignment:NSCenterTextAlignment];
	[titleField setFont:[self font]];
	[titleField setTextColor:[self fontColor]];
	[titleField setDrawsBackground:NO];
	[titleField setBordered:NO];
	[titleField setTarget:self];
	[titleField setAction:@selector(fieldDone:)];
	[titleField setDelegate:self];
	[titleField setAutoresizingMask:NSViewMinYMargin];
	[self addSubview:titleField];
}	


@end
